package com.atguigu.eduorder.controller;

import com.atguigu.commonutils.R;
import com.atguigu.eduorder.service.TPayLogService;
import com.sun.media.sound.SoftTuning;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author cyt
 * @create 2020-06-02 16:39
 */
@RequestMapping("/orderservice/log")
@RestController
@Slf4j
@Api(description = "订单微服务")
public class PlayLogController {
    @Autowired
    TPayLogService payLogService;

    /**
     * 生成 二维码
     *
     * @param orderNo
     * @return
     */
    @GetMapping("/createNative/{orderNo}")
    public R createNative(@PathVariable("orderNo") String orderNo) {
        Map map = payLogService.createNative(orderNo);
        return R.ok().data(map);
    }

    /**
     * 查询支付状态
     * @param id
     * @return
     */
    @GetMapping("/queryPayStatus/{id}")
    public R queryOrder(@PathVariable("id") String id) {
        // 查询订单的信息
        Map<String, String> map = payLogService.queryPayStatus(id);
        System.out.println("支付状态的map: " + map.toString());
        System.out.println("is 欧克 ： " + map.get("trade_state"));
        if (map == null) {
            return R.error().message("支付出错");
        }
        log.info(map.get("trade_state"));
        String tradeState = map.get("trade_state");

        System.out.println(tradeState.equals("SUCCESS"));
        if (tradeState.equals("SUCCESS")) {
            // 更新状态
            payLogService.updateOrderStatus(map);
            return R.ok().message("支付成功");
        }
        return R.ok().code(25000).message("支付中");
    }
}
